/**
* \brief 
* \author pengcheng (pengcheng@yslrpch@126.com)
* \date 2020-05-06
* \attention CopyrightÃ‚Â©ADC Technology(tianjin)Co.Ltd
* \attention Refer to COPYRIGHT.txt for complete terms of copyright notice
*/

#include "stereo_vision/internal.h"

namespace {

	__global__ void cast_16bit_8bit_array_kernel(const uint16_t* arr16bits, uint8_t* arr8bits, int num_elements) {
		int i = blockIdx.x * blockDim.x + threadIdx.x;
		arr8bits[i] = (uint8_t)arr16bits[i];
	}

	__global__ void cast_8bit_16bit_array_kernel(const uint8_t* arr8bits, uint16_t* arr16bits, int num_elements) {
		int i = blockIdx.x * blockDim.x + threadIdx.x;
		arr16bits[i] = (uint16_t)arr8bits[i];
	}

}

namespace adc {
	namespace details {

		void cast_16bit_8bit_array(const uint16_t* arr16bits, uint8_t* arr8bits, int num_elements) {
			for (int mod = 1024; mod != 0; mod >>= 1) {
				if (num_elements % mod == 0) {
					cast_16bit_8bit_array_kernel << <num_elements / mod, mod >> >(arr16bits, arr8bits, num_elements);
					break;
				}
			}
		}

		void cast_8bit_16bit_array(const uint8_t* arr8bits, uint16_t* arr16bits, int num_elements) {
			for (int mod = 1024; mod != 0; mod >>= 1) {
				if (num_elements % mod == 0) {
					cast_8bit_16bit_array_kernel << <num_elements / mod, mod >> >(arr8bits, arr16bits, num_elements);
					break;
				}
			}
		}

	}
}
